<?php

namespace app\api\controller;

use app\admin\model\Banner;
use app\common\controller\Api;

/**
 * 商品收藏接口
 */
class Goodscollect extends Api
{
    //protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function add()
    {
        $model = new \app\api\model\GoodsCollect();

        $data = [
            'user_id'=>$this->auth->id,
            'goods_id'=>input('goods_id',0)
        ];

        $info = \app\api\model\GoodsCollect::get($data);
        if (empty($info)){
            $res = $model->save($data);

            $this->success('请求成功',$res);
        }
        $this->error('失败');
    }

    //清空所有
    public function clear()
    {
        $res = \app\api\model\GoodsCollect::where('user_id',$this->auth->id)->delete();

        $this->success('请求成功',$res);

    }

    public function delete()
    {
        $goods_id = input('goods_id',0);

        $res = \app\api\model\GoodsCollect::where(['goods_id'=>$goods_id,'user_id'=>$this->auth->id])->delete();

        $this->success('请求成功',$res);
    }

    public function iscollect()
    {
        $goods_id = input('goods_id',0);
        $info = \app\api\model\GoodsCollect::get(['user_id'=>$this->auth->id,'goods_id'=>$goods_id]);
        $this->success('请求成功',$info?1:0);
    }

    public function page()
    {
        $lists = \app\api\model\GoodsCollect::alias('b')
            ->join('goods g', 'g.id=b.goods_id', 'LEFT')
            ->where(['b.user_id'=>$this->auth->id])
            ->field('b.id,b.goods_id,b.createtime,g.name,g.image,g.price')
            ->order('b.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('请求成功',$lists);
    }

}
